<template>
  <!-- 公司设置 -->
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border :data="roleList">
              <el-table-column label="序号" width="120" type="index">
                <!-- 索引的改善 -->
                <template #default="{$index}">
                  {{ (page.page-1)*page.pagesize+$index+1 }}
                </template>
              </el-table-column>
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRoleList(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoleList(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <!-- page-sizes要与设置的对上 -->
              <el-pagination
                :page-sizes="[1,2, 3, 5, 8]"
                :page-size="page.pagesize"
                layout="total,sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区不能修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px" :model="fromData">
              <el-form-item label="公司名称">
                <el-input v-model="fromData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="fromData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="fromData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="fromData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <!-- 编辑对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="showDialog"
        width="50%"
        @close="btnCancel"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="btnCancel">取 消</el-button>
          <el-button type="primary" @click="btnOK">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分配权限对话宽 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <el-tree
          ref="permTree"
          :data="permList"
          :props="{label: 'name'}"
          default-expand-all
          show-checkbox
          check-strictly
          node-key="id"
        />
        <template #footer>
          <el-row type="flex" justify="center">
            <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>

import { getCompanyInfo, getRoleList, delRoleList, getRoleListInfo, editRoleList, addRoleList, assignPermission } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { listToTreeData } from '@/utils'

export default {
  data() {
    return {
      fromData: {
        name: '',
        companyAddress: '', // 地址
        mailbox: '', // 邮箱
        remarks: ''// 介绍
      },
      roleList: [], // 角色信息
      page: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      permList: [], // 权限列表
      showPermDialog: false,
      roleId: ''
    }
  },
  computed: {
    title() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getCompanyInfo()
    this.getRoleList()
    this.getPermissionList()// 拿到权限列表
  },
  methods: {
    // 公司信息
    async getCompanyInfo() {
      // console.log(this.$store.getters.id)
      this.fromData = await getCompanyInfo(this.$store.getters.id)
      // console.log(res)
    },
    // 角色信息
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)

      this.roleList = rows
      this.total = total
      console.log(this.roleList)
    },
    // 页码条数
    handleSizeChange(val) {
      this.page.pagesize = val
      this.getRoleList()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.getRoleList()
    },
    // 删除功能
    async  delRoleList(id) {
      // console.log(id)
      const removeconfirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // console.log(removeconfirm)
      if (removeconfirm === 'cancel') return this.$message.info('用户取消删除')
      await delRoleList(id)
      // console.log(this.page.pagesize)
      // console.log(this.roleList)
      // 删除之前还是1条  删除之后才是0条
      if (this.roleList.length === 1 && this.page.page > 1) {
        // console.log(1)
        this.page.page--
      }

      // console.log(this.page)
      this.getRoleList()
      this.$message.success('用户删除成功')
    },
    // 编辑功能
    async  editRoleList(id) {
      this.showDialog = true
      // 拿到数据
      this.roleForm = await getRoleListInfo(id)
    },
    // 点击取消按钮
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 点击确定按钮
    btnOK() {
      this.$refs.roleForm.validate(async val => {
        if (!val) return this.$message.error('表单验证失败')
        // 判断是否有id
        if (this.roleForm.id) {
          // 编辑
          await editRoleList(this.roleForm)
        } else {
          // 新增
          await addRoleList(this.roleForm)
        }
        this.$message.success('操作成功')
        this.showDialog = false
        this.getRoleList()
      })
    },
    // 分配权限
    async  assignPerm(id) {
      this.roleId = id
      const { permIds } = await getRoleListInfo(id)
      this.showPermDialog = true
      // console.log(res)
      this.$nextTick(() => {
        console.log(permIds)
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    async   getPermissionList() {
      const res = await getPermissionList()

      // console.log(res)
      // 过滤  让单前显示的出来
      this.permList = listToTreeData(res.filter(item => item.enVisible === '1'), '0')
    },
    // 点击取消按钮
    btnPermCancel() {
      // 清空数据
      this.$refs.permTree.setCheckedKeys([])
      this.showPermDialog = false
    },
    async btnPermOK() {
      // getCheckedKeys拿到选中id
      await assignPermission({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      this.$message.success('修改成功')
      this.showPermDialog = false
    }

  }
}
</script>

<style>

</style>
